<?php


namespace App\Services;


use App\Goods;
use App\GroupBuy;
use App\GroupBuyItem;
use App\GroupMaster;
use App\Jobs\CancelGroup;
use App\Jobs\CommissionJob;
use App\Jobs\Double11Job;
use App\Jobs\GivingJob;
use App\Jobs\GroupBuyOverJob;
use App\Jobs\ReduceStock;
use App\Merchant;
use App\Order;
use App\Share;
use App\User;
use Carbon\Carbon;
use Illuminate\Support\Facades\Log;

class PayService
{
    public function success($order_number,$pay_query)
    {
        $order = Order::where('order_number',$order_number)->first();
        $user=User::find($order->uid);
         dispatch(new ReduceStock($order_number));
        $order->pay_time = Carbon::now()->toDateTimeString(); // 更新支付时间为当前时间
        $order->status = 1;
        $order->merchant_number=isset($pay_query["transaction_id"])?$pay_query["transaction_id"]:'';
        $order->save();
        $message_service=new MessageService();
        $message_service->send(
            "6hh_TwwI2o1iWbdF2CLctAkRbWGTPDxr9n8JIJy5X3Q",
            "pages/order/order",
            $user->openid,
            [
                'thing1'=>["value"=>"购买商品成功"],
                'character_string2'=>["value"=>$order->order_number],
                'thing3'=>["value"=>"支付成功"]
            ]

        );
        $share=Share::where('order_number',$order->order_number)->first();
        Log::info("share",[$share]);
        if ($order->is_group_buy==1){
            $group_item=GroupBuyItem::where('order_number',$order->order_number)->first();
            $group_item->pay_time=$order->pay_time;
            $group_item->pay_status=1;
            $group_item->status=1;
            $group_master=GroupMaster::find($group_item->group_master_id);
            $group=GroupBuy::find($group_master->group_buy_id);
            $goods=Goods::find($group['goods_id']);
            $group_master->current_num+=1;
            if ($group_master->current_num==$group->max_man){
                $group_master->over=1;
            }
            if ($group_item->is_master===1){
                $group_master->status=1;
            }
            $group_master->save();
            $group_item->save();
            $group_items=GroupBuyItem::where('group_master_id',$group_master->id)->pluck("u_nickname");

            $message_service->send(
                "uY-0XFdSEHb7YnaeprcMQ6JBnTDrUvRzPdQRj1XcbeA",
                "pages/groupon/my",
                $user->openid,
                [
                    'thing1'=>["value"=>"拼团成功"],
                    'character_string2'=>["value"=>$order->order_number],
                    'thing7'=>["value"=>$goods->name],
                    'thing9'=>$group_items->implode(",")
                ]

            );
            if ($group_item->is_master){
                Log::info("开团:",[$group_item]);
                $diff=Carbon::createFromTimeString($group_item->exp_time);
                $job=(new CancelGroup($group_item->group_master_id))->delay($diff);
                $diff=Carbon::createFromTimeString($group_item->exp_time);
//                    $diff=Carbon::createFromTimeString($group_item->exp_time)->diffAsCarbonInterval(Carbon::now());
                dispatch($job);
            }
            if ($group_master->current_num==$group_master->max_man){
                $group_master->over=1;
                $group_master->save();
                dispatch(new GroupBuyOverJob($group_master->id));
            }

        }
        if ($order->share_uid){

            if (!$user->parent_id){
                $parent_user=User::find($order->share_uid);
                if ($parent_user){
                    User::where('id',$order->share_uid)->update(['parent_id'=>$order->share_uid,'level'=>$parent_user->level+1]);
                }
            }
            $merchant=Merchant::where('uid',$order->share_uid)->first();
            $user=User::find($order->share_uid);
            if (!$order->is_group_buy&&$merchant&&$user->is_merchant&&$order->uid!=$order->share_uid){

                dispatch(new CommissionJob($order->id));
            }
            if (!$order->is_group_buy&&!$merchant&&$order->uid!=$order->share_uid&&$share){

                $share->status=1;
                $share->save();
                dispatch(new GivingJob($order));
            }
        }
        dispatch(new Double11Job($order));

        return true;
    }
}
